//
//  ProgressImageView.swift
//  Weibo8023
//
//  Created by styshy on 15/12/20.
//  Copyright © 2015年 sz. All rights reserved.
//

import UIKit

class ProgressImageView: UIImageView {
    
    // 进度提示
    var progress:CGFloat = 0{
        didSet{
            progressView.progress = progress
        }
    }
    init(){
        
        super.init(frame:CGRectZero)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 设置布局
    private func setupUI(){
        addSubview(progressView)
        progressView.backgroundColor = UIColor.clearColor()

        // 自动布局
        progressView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.snp_edges)
        }
    }
    
    // 懒加载控件
    private lazy var progressView:ProgressView = ProgressView()
}


/// 进度视图
private class ProgressView:UIView{
    var progress:CGFloat = 0{
        didSet{
            self.setNeedsDisplay()
        }
    }
    
    private override func drawRect(rect: CGRect) {
        // 中心点
        let center = CGPoint(x: rect.width * 0.5, y: rect.height * 0.5)
        // 半径
        let radius = min(rect.size.width, rect.size.height) * 0.5
        // 起始角度
        let startAngle = CGFloat(-M_PI_2)
        // 结束角度
        let endAngle = startAngle + progress * CGFloat(M_2_PI)
        
        
        let path = UIBezierPath(arcCenter: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
        // 形成闭环
        path.addLineToPoint(center)
        
        UIColor(white: 0.5, alpha: 1.0).setFill()
        
        path.fill()
    }
}
